
; This source file is borrowed in its entirety from Tokumaru's decoder.

	;place the header immediately before the program banks
	.org $7ff0

	;indicate the presence of this header
	.db "NES", $1a

	;indicate the number of 16KB PRG-ROM pages
	.db $01

	;indicate the number of 8KB CHR-ROM pages
	.db $00

	;indicate an UxROM board with vertical mirroring
	.db $21

	;clear the remaining bytes
	.pad $8000, $00

	;variables in page 0
	.enum $0000

BaseMemory0 ;marks the start of free memory in page 0

	.ende

	;set the starting address of the bank
	.base $c000

	;include the decompression function
	.include "deack.asm"

Reset:

	;disable interrupts and decimal mode
	sei
	cld

	;disable rendering and vertical blank interrupts
	ldx #$00
	stx $2000
	stx $2001

	;initialize the stack
	dex
	txs

	;disable APU Frame IRQs
	lda #$40
	sta $4017

	;disable DMC IRQs
	lda #$00
	sta $4010

	;wait for the PPU to become stable
	bit $2002
-	bit $2002
	bpl -
-	bit $2002
	bpl -

	;set the destination address of the palette
	lda #$3f
	sta $2006
	lda #$00
	sta $2006

	;upload a basic palette
	lda #$0B
	sta $2007
	lda #$1B
	sta $2007
	lda #$34
	sta $2007
	lda #$30
	sta $2007

	;set the source address of the tiles
	lda #<CompressedTiles
	sta ACK_InputData+0
	lda #>CompressedTiles
	sta ACK_InputData+1

	;set the destination address of the tiles
	lda #$00
	sta $2006
	sta $2006

	;decompress the tiles
	jsr ACK_Decompress

	;prepare to draw the contents of the first name table
	lda #$20
	sta $2006
	lda #$00
	sta $2006

	;fill the first name table with tiles
	lda #$00
	ldy #$04
--	ldx #$f0
-	sta $2007
	clc
	adc #$01
	dex
	bne -
	dey
	bne --

	;clear the corresponding attribute table
	lda #$00
	ldx #$40
-	sta $2007
	dex
	bne -

	;wait for VBlank
	bit $2002
-	bit $2002
	bpl -

	;reset the scroll
	lda #$00
	sta $2000
	sta $2005
	sta $2005

	;enable rendering
	lda #%00001010
	sta $2001

LoopForever:

	;loop forever
	jmp LoopForever

NMI:
IRQ:

	;return from the interrupt
	rti

	.dsb 6
CompressedTiles:

	;include a compressed group of tiles
	.incbin "battlekid.chr.ack"

	;place the vectors at the end of the bank
	.org $fffa

	.dw NMI
	.dw Reset
	.dw IRQ
